import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push;
 
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
    
const routes = [
  {
    path: '/home',
    name: 'home',
    meta:{show:0},
    component: HomeView,
    beforeEnter: (to, from,next) => {
      // reject the navigation
      //判断是否有登录，如果有则直接跳转到首页，如果没有就跳转到登录页
      let token = localStorage.getItem('shuchotaitoken')
      setTimeout(() => {
              if(token){
        next()
      }else{
        next('/login')
      } 
      }, 100);
       if(token){
        next()
      }else{
        next('/login')
      }
    },
  },
  {
    path:'/classify',
    meta:{name:'分类管理',show:1},
    component: () => import(/* webpackChunkName: "about" */ '../views/classify.vue')
  },
  {
    path:'/usermanagement',
    meta:{name:'用户管理',show:2},
    component:() => import(/* webpackChunkName: "about" */ '../views/usermanagement.vue')
  },
  {
    path:'/goods',
    meta:{name:'商品管理',show:3},
    component:() => import(/* webpackChunkName: "about" */ '../views/goods.vue')
  },
  {
    path:'/login',
    name:'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
  },
    //重定向
    {
      path:'/',
      redirect:'/home'
    },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  document.body.scrollTop = 0
  // firefox
  document.documentElement.scrollTop = 0
  next()
})
export default router
